public class Complex {
    private double real;
    private double imag;
    public Complex(){
        this.real=0.0;
        this.imag=0.0;
    }
    public Complex(double real,double imag){
        if(Double.isNaN(real)){
            System.out.println("实部是NaN，已默认设为零");
            real=0.0;
        }
        if(Double.isNaN(imag)){
            System.out.println("虚部是NaN，已默认设为0.0i");
            imag=0;
        }
        this.real=real;
        this.imag=imag;
    }
    public double getReal(){
        return real;
    }
    public double getImag(){
        return imag;
    }
    public void setReal(double real){
        if(Double.isNaN(real)){
            System.out.println("你输入了NaN，已默认设为0.0");
            real=0.0;
        }
        this.real=real;
    }
    public void setImag(double imag){
        if(Double.isNaN(imag)){
            System.out.println("你输入了NaN，已默认设为0.0i");
            imag=0.0;
        }
        this.imag=imag;
    }
    public void setValue(double real,double imag){
        if(Double.isNaN(real)){
            System.out.println("你输入的实部是NaN，已默认设为0.0");
            real=0.0;
        }
        if(Double.isNaN(imag)){
            System.out.println("你输入的虚部是NaN，已默认设为0.0i");
            imag=0.0;
        }
        this.setReal(real);
        this.setImag(imag);
    }

    @Override
    public String toString() {
        return "("+this.real+" + "+this.imag+"i"+")";
    }
    public boolean isReal(){
        return this.imag==0.0;
    }
    public boolean isImaginary(){
        return (this.real==0.0)&&(!(this.imag==0.0));
    }

    @Override
    public boolean equals(Object obj) {
        Complex complex=(Complex) obj;
        return (this.real==complex.real)&&(this.imag==complex.imag);
    }
    public boolean equals(double real, double imag){
        if(Double.isNaN(real)||Double.isNaN(imag)){
            System.out.println("你输入的复数含有NaN，无法比较");
            return false;
        }
        return (this.real==real)&&(this.imag==imag);
    }
    public double abs(){
        return Math.sqrt(this.real*this.real+this.imag*this.imag);
    }
    public Complex add(Complex complex){
        Complex result=new Complex(this.real+complex.real,this.imag+complex.imag);
        return result;
    }
    public Complex substract(Complex complex){
        Complex result=new Complex(this.real-complex.real,this.imag-complex.imag);
        return result;
    }
    public Complex multiply(Complex complex){
        Complex result=new Complex((this.real*complex.real-this.imag*complex.imag),(this.real*complex.imag+complex.real*this.imag));
        return result;
    }
    public Complex divide(Complex complex){
        if((complex.real*complex.real+complex.imag*complex.imag)==0.0){
            System.out.println("不能除以0,已将本次计算的结果默认设为0.0+0.0i");
            Complex result=new Complex();
            return result;
        }
        Complex temp=new Complex(complex.real/(complex.real*complex.real+complex.imag*complex.imag),-(complex.imag)/(complex.real*complex.real+complex.imag*complex.imag));
        Complex result=this.multiply(temp);
        return result;
    }
}
